package org.xg.study.spring.al.config;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;

import java.util.Map;

/**
 * @author CMG
 * @version 1.0
 * @description
 * @cLassName BaseController
 * @date 2021/10/28 9:54
 **/
public abstract class BaseController {
    public static final String CODE = "code";
    public static final String MSG = "msg";
    public static final String DATA = "data";

    public static String success() {
        final Map<String, Object> result = Maps.newHashMap();
        result.put(CODE, RestCodeEnum.SUCCESS.getCode());
        result.put(MSG, RestCodeEnum.SUCCESS.getMsg());
        return JSONObject.toJSONString(result);
    }

    public static String success(Object data) {
        final Map<String, Object> result = Maps.newHashMap();
        result.put(CODE, RestCodeEnum.SUCCESS.getCode());
        result.put(MSG, RestCodeEnum.SUCCESS.getMsg());
        result.put(DATA, data);
        return JSONObject.toJSONString(result);
    }

    public static String error(RestCodeEnum code) {
        final Map<String, Object> result = Maps.newHashMap();
        result.put(CODE, code.getCode());
        result.put(MSG, code.getMsg());
        return JSONObject.toJSONString(result);
    }

    public static String error(RestCodeEnum code, String msg) {
        final Map<String, Object> result = Maps.newHashMap();
        result.put(CODE, code.getCode());
        result.put(MSG, msg);
        return JSONObject.toJSONString(result);
    }
}
